package com.controller;

import com.pojo.Order;
import com.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @RequestMapping("/getUserId")
    public void getUserId(HttpServletRequest request){
        //HttpServletRequest是Servlet的请求对象
            String userId = request.getParameter("userId");
            System.out.println("Userid: " + userId);
        }
        @RequestMapping("/getUserMapping")
    public void getUserAndId(String username,Integer id){
        System.out.println("username: " + username);
        System.out.println("id: " + id);
        }

        @RequestMapping("/getUserName")
    public void getUserName(
                @RequestParam( value="name" , required = false, defaultValue = "itheima") String username
        ){
            System.out.println("username: " + username);
    }

    //pojo绑定
    @RequestMapping("/registerUser")
    public void registerUser(User user){
        String name = user.getName();
        String password = user.getPassword();
        System.out.println("name: " + name);
        System.out.println("password: " + password);
    }
    @RequestMapping("/findOrderWithUser")
    public void fin(User user){
        String username = user.getName();
        String orderId = user.getOrder().getOrderId();
        System.out.println("username: " + username);
        System.out.println("orderId: " + orderId);
    }
    @RequestMapping("/showOrders")
    public void showOrders(User user){
        List<Order> ordersList = user.getOrders();
        List<String> addressList = user.getAddress();
        System.out.println("订单");
        for(int i=0;i< ordersList.size();i++){
            Order order = ordersList.get(i);
            String address = addressList.get(i);
            System.out.println("订单ID："+order.getOrderId());
            System.out.println("订单配送地址："+address);
        }
    }


    @RequestMapping("/user")
    public String user(Model model){
        User user= new User();
        user.setName("张三");
        user.setPassword("123456");
        model.addAttribute("user,user");
        return "user";
    }

}
